package com.lovo.ssc.controller;


import com.lovo.dto.CommodityDto;
import com.lovo.dto.PendingCommodityDto;
import com.lovo.ssc.bean.Commodity;
import com.lovo.ssc.bean.Pendingcommodity;
import com.lovo.ssc.dao.IWebCommodityDao;
import com.lovo.ssc.dto.WebCommodityDto;
import com.lovo.ssc.service.ICommodityService;
import com.lovo.sscsys.remoteService.IsscsysRemoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 0-已上架、1-促销中、2-预售商品 商品控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-11
 */
@RestController
@RequestMapping("/commodity")
@Api(tags = "商品应用控制器002--------刘文龙")
@Slf4j
public class CommodityController {
	@Autowired
	ICommodityService commodityService;

	@Autowired
	IWebCommodityDao webCommodityDao;



	/**
	 * 从商城后台拉取已上架商品
	 *
	 * @param commodityDtoList 已上架商品dto集合
	 */
	@PostMapping("findAllCommodityBySscSys")
	public void findAllCommodityBySscSys(@RequestBody List<CommodityDto> commodityDtoList) {
		//清空本地已上架商品数据库
		commodityService.trunCateCommodity();
		for (CommodityDto c : commodityDtoList) {
			//数据拷贝
			Commodity commodity = new Commodity();
			commodity.setCommoditycode(c.getCommoditycode());
			commodity.setCommodityname(c.getCommodityname());
			commodity.setCommoditybatch(c.getCommoditybatch());
			commodity.setNorms(c.getNorms());
			commodity.setType(c.getType());
			commodity.setNumber(c.getNumber());
			commodity.setPrice(Double.valueOf(String.valueOf(c.getPrice())));
			commodity.setState(c.getState());
			//保存到本地数据库
			commodityService.save(commodity);
		}
	}


	@ApiOperation(value = "根据商品状态、类型从缓存中查询商品")
	@GetMapping("findCommodityByState")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(required = true, name = "state", value = "商品状态,0--查全部，1--查促销"),
			@ApiImplicitParam(required = false, name = "type", value = "商品类型")
	})

	public Object findCommodityByState(@RequestParam String state,
	                                   @RequestParam("type") String type) {
		if ("undefined".equals(type) || "".equals(type)) {
			type = null;
		}

		try {
			if ("1".equals(state)) {
				log.info("查促销商品   " + state + type);
				//查询促销商品
				return commodityService.findCommodity(state, type);
			} else if ("0".equals(state)) {
				log.info("查所有商品   " + state + type);
//				log.info("1111"+null);
//				log.info(String.valueOf(type.equals("undefined")));
				//查询所有商品
				return commodityService.findCommodity(state, type);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}


	@ApiOperation(value = "从es获取商品信息")
	@GetMapping("/findAllByES")
	public List<WebCommodityDto> findAllByES(@RequestParam() String commodityDescribe) {
		return webCommodityDao.findAllByCommoditydescribe(commodityDescribe);
	}


//	@ApiOperation(value = "从es获取商品信息")
//	@GetMapping("/findAllByES")
//	public

}
